package com.example.canteen.web.servlet;

import com.example.canteen.domain.Admin;
import com.example.canteen.domain.ResultInfo;
import com.example.canteen.domain.Student;
import com.example.canteen.service.StudentService;
import com.example.canteen.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "StudentServlet", value = "/Student/*")
public class StudentServlet extends BaseServlet{
    private StudentService studentService = new StudentServiceImpl();
    //注册
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //封装对象
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
//        System.out.println(student);
        //调用service完成注册
//        UserServiceImpl userService = new UserServiceImpl();
        boolean flag = studentService.register(student);
        ResultInfo info = new ResultInfo();
        //响应结果
        if(flag){
            info.setFlag(true);
        }
        else{
            info.setFlag(false);
            info.setErrorMsg("注册失败");
        }

        writeValue(info,response);
    }
    //登录
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        Student s = studentService.login(student.getId(),student.getPassword());

        ResultInfo info = new ResultInfo();
        if(s==null){
            info.setFlag(false);
            info.setErrorMsg("用户名密码错误");
        }
        else{
            request.getSession().setAttribute("student",s);
            info.setFlag(true);
        }
//        //封装数据
//        ObjectMapper mapper = new ObjectMapper();
//        response.setContentType("application/json;charset=utf-8");
//        mapper.writeValue(response.getOutputStream(),info);
        writeValue(info,response);
    }
    //查找一个
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从session获取用户
        Object student = request.getSession().getAttribute("student");
        writeValue(student,response);
    }
    //删除
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        boolean b = studentService.delete(Integer.valueOf(id));

        ResultInfo info = new ResultInfo();
        if (!b){
            info.setFlag(false);
            info.setErrorMsg("删除失败");
        }
        else{
            info.setFlag(true);
        }
        //封装数据
//        ObjectMapper mapper = new ObjectMapper();
//        response.setContentType("application/json;charset=utf-8");
//        mapper.writeValue(response.getOutputStream(),info);
        writeValue(info,response);
    }
    //查找所有
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Student> all = studentService.findAll();
        writeValue(all,response);
    }
    //更新
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //封装对象
        Student student = new Student();
        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = studentService.update(student);
        ResultInfo info = new ResultInfo();
        if (!b){
            info.setFlag(false);
            info.setErrorMsg("更新失败");
        }
        else{
            info.setFlag(true);
        }

        writeValue(info,response);
    }

    public void showOne(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        String id = request.getParameter("id");
        Student admin = studentService.findById(Integer.valueOf(id));
        writeValue(admin,response);
    }

}
